import os
import sys
import pandas as pd
import datetime
from MainConsolidator import MainConsolidator

class handler(object):
    @staticmethod
    def module_name():
        return 'Труба профильная ТЭСЦ-2'
    @staticmethod
    def handle(row:pd.Series,consolidator:MainConsolidator)->pd.Series:
        product_up = None
        try:
            wall = row["Толщина стенки, мм <Производство>"]
            s1 = str(row['Размер профиля 1, мм <Производство>']).replace(".0","")
            s2 = str(row['Размер профиля 2, мм <Производство>']).replace(".0","")
            size = f"{s1}Х{s2}"
            thickness_range = consolidator.scalar_in_guid("Параметры по Продукту ТЭСЦ-2",
                f"`Производство`=='Труба профильная ТЭСЦ-2' and `Доп параметр1`=='{size}' and {wall}>=`Мин` and {wall}<=`Макс`","Диапазон")
            print(f"t2 wall: {wall} ; size: {size}; thickness_range: {thickness_range}")
            product_up = f"ТРУБА Д{size} {thickness_range} ГУ"
            row["error"] = pd.NA
        except Exception as exp: 
            row["error"] = f"{exp}"
        finally:
            row["Продукт УП"]= product_up
            row["Время укрупнения"]=datetime.datetime.now()
            return row
        
    def debug_handle(self,inputDf:pd.DataFrame,consolidator:MainConsolidator,module:str=None)->pd.DataFrame:
        try:
            if module is None: return inputDf.apply(lambda row: self.handle(row,consolidator), axis=1)
            else: return inputDf[inputDf["Производство2"]==module].apply(lambda row: self.handle(row,consolidator), axis=1)
        except: raise
    
    def get_compiled(self):
        code:str=f"print('{__file__}')"
        try:
            with open(__file__,encoding="utf-8",mode="r") as f:
                code=f.read()
        except Exception as exp: 
            code=f"print('error: {exp}')"
        finally:
            cc = compile(code,"","exec")
            return cc

if __name__=="__pipe_profile_t2__":
    consolidator:MainConsolidator
    record:pd.Series
    result:pd.Series
    result = handler.handle(record,consolidator)

